import Vue from 'vue'
import VueRouter from "vue-router";
import Home from "@/views/home/Home";
import Search from "@/views/Search";
import Login from "@/views/Login";
import Register from "@/views/Register";
import Detail from "@/views/Detail"
import AddSuccess from "@/views/AddCartSuccess/AddSuccess";
import ShopCart from "@/views/ShopCart/ShopCart";
import da from "element-ui/src/locale/lang/da";
import Pay from "@/views/Pay/Pay";
import Personal from "@/views/Personal/Personal";
import PaySuccess from "@/views/Pay/PaySuccess";
import MyOrder from "@/views/Order/MyOrder";
import ChatInfo from "@/views/chat/ChatInfo";
import MyComplaint from "@/views/Complaint/MyComplaint";
//使用插件
Vue.use(VueRouter)
//测试
// const originPush=VueRouter.prototype.push();
// const originReplace=VueRouter.prototype.replace();
//第一个参数告诉往哪里条，第二个参数成功的回调，第三哥参数失败的回调
// VueRouter.prototype.push=function (location,resolve,reject) {
//     if (resolve&&reject){
//         //call和apply区别：都可以调用函数一次篡改函数上下文一次
//         //call与apply区别：call传递参数可以逗号隔开，apply方法执行传递数组
//         VueRouter.prototype.push().call(this,location,resolve,reject)
//     }else {
//         VueRouter.prototype.push().call(this,location,()=>{},()=>{})
//     }
// }
// VueRouter.prototype.replace=function (location,resolve,reject) {
//     if (resolve&&reject){
//         //call和apply区别：都可以调用函数一次篡改函数上下文一次
//         //call与apply区别：call传递参数可以逗号隔开，apply方法执行传递数组
//         VueRouter.prototype.replace().call(this,location,resolve,reject)
//     }else {
//         VueRouter.prototype.replace().call(this,location,()=>{},()=>{})
//     }
// }
let router= new VueRouter({
    //配置路由
    mode: 'history',
    routes:[
        {
            path: '/',
            redirect: '/home',

        },
        {
            path:"/home",
            component:Home,
            meta:{show:true,title: '好未来住房'}
        },
        {
            path:"/search/:keyword",
            component:Search,
            meta:{show:true,title: '好未来住房'},
            name:"search"
        },
        {
            path:"/login",
            component:Login,
            meta:{show:false,title: '好未来住房-登录'}
        },
        {
            path:"/register",
            component:Register,
            meta:{show:false,title: '好未来住房-注册'}
        },
        {
            path:"/detail/:id",
            component:Detail,
            meta:{show:true,title: '好未来住房'}
        },
        {
            path:"/addsuccess",
            name:'addsuccess',
            component:AddSuccess,
            meta:{show:true,title: '好未来住房'}
        },
        {
            path:"/shopcart",
            name:'shopcart',
            component:ShopCart,
            meta:{show:true,title: '好未来住房'}
        },
        {
            path:"/pay",
            name:'pay',
            component:Pay,
            meta:{show:true,title: '好未来住房'}
        },
        {
            path:"/personal",
            name:'personal',
            component:Personal,
            meta:{show:true,title: '好未来住房'}
        },
        {
            path:"/paysuccess",
            name:'paysuccess',
            component:PaySuccess,
            meta:{show:true,title: '好未来住房'}
        },
        {
            path:"/myorder",
            name:'myorder',
            component:MyOrder,
            meta:{show:true,title: '好未来住房'}
        },
        {
            path:"/complaint",
            name:'complaint',
            component:MyComplaint,
            meta:{show:true,title: '好未来住房'}
        },
        {
            path:"/chatinfo",
            name:'chatinfo',
            component:ChatInfo,
            meta:{show:true,title: '好未来住房'}
        },
    ],
    //滚动条在最上方
    scrollBehavior (to, from, savedPosition) {
        return { y: 0 }
    }
})
//全局守卫，前置守卫(在路由跳转之间判断)
router.beforeEach((to, from, next) => {
    //to:可以获取到你要跳转的哪个路由信息
    //form：可以获取到你从哪个路由而来的信息
    //next:放行函数，next()放行
    if (to.meta.title) {
        document.title = to.meta.title
    }
    next();
    let data= JSON.parse(sessionStorage.getItem("LoginInfo"));
    //用户登录了
    if(data){
        //想去登录界面
        if(to.path=='/login'|| to.path=='/register'){
            next('/')
        }
    }else{
        //未登录
        if(to.path=='/addsuccess'|| to.path=='/shopcart'){
            next('/login')
        }
    }

});
export default router
